package dmt16.TelemetrySuit.UI.PlotTools
{
	import flash.display.Shape;
	
	public class GraphLine extends Shape
	{
		public function GraphLine(yData:Vector.<Number>,xData:Vector.<Number>=null)
		{
			this.yData=yData;
			this.xData=xData;
			cacheAsBitmap=true;
		}
		
		public  var xData:Vector.<Number>;
		public  var yData:Vector.<Number>;
		
		public var lineStyle:GraphLineStyle=new GraphLineStyle(1,0xFF33CCFF);
		
		public function get maxX():Number{
			if(yData.length==0)
				return 0;
			if(xData==null)
				return yData.length;
			var _maxX:Number=xData[0];
			for(var i:Number=0;i<xData.length;i++)
				_maxX=xData[i]>_maxX?xData[i]:_maxX;
			return _maxX;
		}
		
		
		public function get maxY():Number{
			if(yData.length==0)
				return 0;
			var _maxY:Number=yData[0];
			for(var i:Number=0;i<yData.length;i++)
				_maxY=yData[i]>_maxY?yData[i]:_maxY;
			return _maxY;
		}
		
		public function get minX():Number{
			if(yData.length==0)
				return 0;
			if(xData==null)
				return 0;
			var _minX:Number=xData[0];
			for(var i:Number=0;i<yData.length;i++)
				_minX=xData[i]<_minX?xData[i]:_minX;
			return _minX;
		}
		
		public function get minY():Number{
			if(yData.length==0)
				return 0;
			var _minY:Number=yData[0];
			for(var i:Number=0;i<yData.length;i++)
				_minY=yData[i]<_minY?yData[i]:_minY;
			return _minY;
		}
		
		private var _visible:Boolean=true;
		public override function get visible():Boolean{
			return _visible;
		}
		public override function set visible(value:Boolean):void{
			_visible=value;
			super.visible=value;
			graphics.clear();
		}
		
		public function draw(scaleX:Number,scaleY:Number):void{
			graphics.clear();
			graphics.lineStyle(lineStyle.thickness,
				lineStyle.color,
				lineStyle.alpha,
				lineStyle.pixelHinting,
				lineStyle.scaleMode,
				lineStyle.caps,
				lineStyle.joints,
				lineStyle.miterLimit);
			var i:Number;
			if(yData.length>0){
				if(xData!=null){
					var p:Number=Math.min(xData.length,yData.length);
					graphics.moveTo(xData[0]*scaleX,-yData[0]*scaleY);
					for(i=1;i<p;i++){
						graphics.lineTo(xData[i]*scaleX,-yData[i]*scaleY);
					}
				}
				else{
					graphics.moveTo(0,-yData[0]*scaleY);
					for(i=1;i<yData.length;i++){
						graphics.lineTo(i*scaleX,-yData[i]*scaleY);
					}
				}
			}
		}
	}
}